package com.jinmdz.fmis.dao.admin.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 行政区划Provider
 *
 * @author LiCongLu
 * @date 2020-03-15 9:09
 */
public class AdministrativeAreaProvider {

    /**
     * 按照父主键查询行政区划列表
     *
     * @param parentId 父类主键
     * @return
     * @author LiCongLu
     * @date 2020-03-15 9:12
     */
    public String listAdministrativeAreaListByParentId(@Param("parentId") Integer parentId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" system_area.id,system_area.parent_id,system_area.area_code,system_area.area_name,system_area.area_name_abbr");
                builder.append(" ,system_area.disable_state,system_area.as_province_city_county,system_area.sort,system_area.remark,system_area.version ");
                builder.append(" ,parent_area.area_code as parent_code,parent_area.area_name as parent_name");
                SELECT(builder.toString());
                FROM(" system_area ");
                LEFT_OUTER_JOIN(" system_area parent_area on parent_area.id = system_area.parent_id ");
                WHERE(" system_area.deleted = 0 ");
                WHERE(" system_area.parent_id = #{parentId} ");
                ORDER_BY(" system_area.area_code ");
            }
        }.toString();
    }

    /**
     * 按照主键获取行政区划信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-15 9:12
     */
    public String getAdministrativeAreaEditById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" system_area.id,system_area.parent_id,system_area.area_code,system_area.area_name,system_area.area_name_abbr");
                builder.append(" ,system_area.disable_state,system_area.as_province_city_county,system_area.sort,system_area.remark,system_area.version ");
                builder.append(" ,parent_area.area_code as parent_code,parent_area.area_name as parent_name");
                SELECT(builder.toString());
                FROM(" system_area ");
                LEFT_OUTER_JOIN(" system_area parent_area on parent_area.id = system_area.parent_id ");
                WHERE(" system_area.deleted = 0 ");
                WHERE("  system_area.id = #{id} ");
                ORDER_BY(" system_area.area_code ");
            }
        }.toString();
    }
}
